<?php
/*
	[Destoon B2B System] Copyright (c) 2008-2010 Destoon.COM
	This is NOT a freeware, use is subject to license.txt
*/
@set_time_limit(0);
define('DT_NONUSER', true);
define('DT_ADMIN', true);
require '../common.inc.php';
require DT_ROOT.'/include/module.func.php';
require DT_ROOT.'/include/cache.func.php';
require DT_ROOT.'/admin/global.func.php';
require DT_ROOT.'/include/sql.func.php';
$step = isset($step) ? $step : 1;
$db->halt = 0;
switch($step) {
	case '1'://说明
		if(DT_RELEASE < '20100802') {
			$msg = '升级无法进行，请按升级说明正确覆盖文件';
			include 'msg.tpl.php';
			exit;
		}
		if(!is_writeable(DT_ROOT.'/config.inc.php')) {
			$msg = '升级无法进行，设置根目录下config.inc.php属性为可写';
			include 'msg.tpl.php';
			exit;
		}
		$CFG = array();
		include DT_ROOT.'/config.inc.php';
		$DCFG = $CFG;
		$CFG = array();
		copy(DT_ROOT.'/config.inc.php', DT_CACHE.'/config.inc.php');	
		file_put(DT_ROOT.'/config.inc.php', file_get(DT_ROOT.'/upgrade/config.inc.php'));		
		include DT_ROOT.'/config.inc.php';
		foreach($CFG as $k=>$v) {
			if(isset($DCFG[$k])) $CFG[$k] = $DCFG[$k];
		}
		$CFG['template'] = 'default';
		$CFG['skin'] = 'default';
		$content = file_get(DT_ROOT.'/config.inc.php');
		foreach($CFG as $k=>$v) {
			$content = preg_replace("/[$]CFG\['$k'\]\s*\=\s*[\"'].*?[\"']/is", "\$CFG['$k'] = '$v'", $content);
		}
		if(!$content) {
			$msg = '升级无法进行，设置根目录下config.inc.php属性为可写<br/>如果config.inc.php损坏，请在cache/目录寻找备份';
			include 'msg.tpl.php';
			exit;
		}
		file_put(DT_ROOT.'/config.inc.php', $content);
		file_del(DT_ROOT.'/file/backup/backup.dat');
		file_del(DT_ROOT.'/js.php');
		file_del(DT_ROOT.'/captcha.png.php');
		file_del(DT_ROOT.'/task.js.php');
		@copy(DT_CACHE.'/install.dat', DT_CACHE.'/install.lock');
		file_del(DT_CACHE.'/install.dat');
		$percent = '0%';
		include 'step_'.$step.'.tpl.php';
	break;
	case '2'://更新数据库
		//运行mysql.sql
		sql_execute(file_get_contents('update.sql'));

		$result = $db->query("SELECT * FROM {$DT_PRE}style WHERE groupid<>''");
		while($r = $db->fetch_array($result)) {
			if(substr($r['groupid'], 0, 1) == ',') continue;
			$groupid = ','.$r['groupid'].',';
			$db->query("UPDATE {$DT_PRE}style SET groupid='$groupid' WHERE itemid=$r[itemid]");
		}

		$result = $db->query("SELECT * FROM {$DT_PRE}message WHERE groupids<>''");
		while($r = $db->fetch_array($result)) {
			if(substr($r['groupids'], 0, 1) == ',') continue;
			$groupids = ','.$r['groupids'].',';
			$db->query("UPDATE {$DT_PRE}message SET groupids='$groupids' WHERE itemid=$r[itemid]");
		}

		$result = $db->query("SELECT * FROM {$DT_PRE}finance_pay");
		while($r = $db->fetch_array($result)) {
			if($r['item']) {
				$pid = $r['pid'];
				list($mid, $id) = explode('-', $r['item']);
				if(substr($r['item'], -1) == '-') $mid = -9;
				$db->query("UPDATE {$DT_PRE}finance_pay SET moduleid='$mid',itemid='$id' WHERE pid=$pid");
			}
		}
		$db->query("ALTER TABLE `{$DT_PRE}finance_pay` DROP `item`");

		$result = $db->query("SELECT userid,mynote FROM {$DT_PRE}company_data WHERE mynote<>''");
		while($r = $db->fetch_array($result)) {
			file_put(DT_ROOT.'/file/user/'.dalloc($r['userid']).'/'.$r['userid'].'/note.php', '<?php exit;?>'.$r['mynote']);			
		}
		$db->query("ALTER TABLE `{$DT_PRE}company_data` DROP `mynote`");

		//更新广告
		$result = $db->query("SELECT * FROM {$DT_PRE}ad WHERE typeid=5");
		while($r = $db->fetch_array($result)) {
			$pid = $r['pid'];
			$ads = -1;
			$aid = $r['aid'];
			unset($r['aid']);
			if($r['code']) {
				$code = explode("\n", trim($r['code']));
				$r['code'] = '';
				foreach($code as $c) {
					$c = explode("|", trim($c));
					$r['listorder'] = $c[0];
					$r['url'] = $r['image_url'] = $c[1];
					$r['image_src'] = $c[2];
					$post = daddslashes($r);
					$sqlk = $sqlv = '';
					foreach($post as $k=>$v) {
						$sqlk .= ','.$k; $sqlv .= ",'$v'";
					}
					$sqlk = substr($sqlk, 1);
					$sqlv = substr($sqlv, 1);
					$db->query("INSERT INTO {$DT_PRE}ad ($sqlk) VALUES ($sqlv)");
					$ads++;
				}
			}
			$db->query("UPDATE {$DT_PRE}ad_place SET `ads`=`ads`+$ads WHERE pid=$pid");
			$db->query("DELETE FROM {$DT_PRE}ad WHERE aid=$aid");
		}
		$result = $db->query("SELECT * FROM {$DT_PRE}ad WHERE typeid=6");
		while($r = $db->fetch_array($result)) {
			$pid = $r['pid'];
			$ads = -1;
			$aid = $r['aid'];
			unset($r['aid']);
			if($r['key_id']) {
				$ids = explode(" ", trim($r['key_id']));
				foreach($ids as $c) {
					$r['key_id'] = $c;
					$post = daddslashes($r);
					$sqlk = $sqlv = '';
					foreach($post as $k=>$v) {
						$sqlk .= ','.$k; $sqlv .= ",'$v'";
					}
					$sqlk = substr($sqlk, 1);
					$sqlv = substr($sqlv, 1);
					$db->query("INSERT INTO {$DT_PRE}ad ($sqlk) VALUES ($sqlv)");
					$ads++;
				}
			}
			$db->query("UPDATE {$DT_PRE}ad_place SET `ads`=`ads`+$ads WHERE pid=$pid");
			$db->query("DELETE FROM {$DT_PRE}ad WHERE aid=$aid");
		}
		$db->query("ALTER TABLE `{$DT_PRE}ad` CHANGE `key_id` `key_id` BIGINT( 20 ) UNSIGNED NOT NULL");
		$r = $db->get_one("SELECT MAX(aid) AS maxid FROM `{$DT_PRE}ad`");
		$maxid = $r['maxid'];
		$maxid++;
		$db->query("INSERT INTO `{$DT_PRE}ad` VALUES ($maxid, '首页展会A1', 5, 3, 0, '', '', '', 0, 'destoon', 1244644047, 'destoon', 1244644397, 1262275200, 1577894399, 0, '', '', '', '', '', 'skin/default/image/150x60.gif', '', '', '', '', 0, 0, '', 0, 0, 3)");
		$maxid++;
		$db->query("INSERT INTO `{$DT_PRE}ad` VALUES ($maxid, '首页展会A2', 6, 3, 0, '', '', '', 0, 'destoon', 1244644047, 'destoon', 1244644397, 1262275200, 1577894399, 0, '', '', '', '', '', 'skin/default/image/150x60.gif', '', '', '', '', 0, 0, '', 0, 0, 3)");
		foreach($MODULE as $k=>$v) {
			if($v['module'] == 'article') {
				$db->query("ALTER TABLE `{$DT_PRE}article_{$v[moduleid]}` DROP INDEX `keyword`");
				$db->query("ALTER TABLE `{$DT_PRE}article_{$v[moduleid]}` ADD INDEX `catid`");
				$db->query("ALTER TABLE `{$DT_PRE}article_{$v[moduleid]}` ADD INDEX `username`");
				$db->query("ALTER TABLE `{$DT_PRE}article_{$v[moduleid]}` CHANGE `ip` `ip` VARCHAR( 50 ) NOT NULL");
				$db->query("ALTER TABLE `{$DT_PRE}article_data_{$v[moduleid]}` DROP INDEX `itemid` , ADD PRIMARY KEY ( `itemid`)");
			} else if($v['module'] == 'info') {
				$db->query("ALTER TABLE `{$DT_PRE}info_{$v[moduleid]}` DROP INDEX `keyword`");
				$db->query("ALTER TABLE `{$DT_PRE}info_{$v[moduleid]}` ADD INDEX `catid`");
				$db->query("ALTER TABLE `{$DT_PRE}info_{$v[moduleid]}` ADD INDEX `areaid`");
				$db->query("ALTER TABLE `{$DT_PRE}info_{$v[moduleid]}` ADD INDEX `editdate` ( `editdate` , `vip` , `edittime` )");
				$db->query("ALTER TABLE `{$DT_PRE}info_{$v[moduleid]}` ADD `ali` VARCHAR( 30 ) NOT NULL AFTER `qq` , ADD `skype` VARCHAR( 30 ) NOT NULL AFTER `ali`");
				$db->query("ALTER TABLE `{$DT_PRE}info_{$v[moduleid]}` CHANGE `ip` `ip` VARCHAR( 50 ) NOT NULL");
				$db->query("ALTER TABLE `{$DT_PRE}info_data_{$v[moduleid]}` DROP INDEX `itemid` , ADD PRIMARY KEY ( `itemid`)");
			}
		}

		$percent = '25%';
		include 'step_'.$step.'.tpl.php';
	break;
	case '3'://安装新增模型	
		sql_execute(file_get_contents('install.sql'));
		
		$db->query("DELETE FROM {$DT_PRE}module WHERE moduleid=11");
		$url = DT_URL.'special/';
		$db->query("INSERT INTO `{$DT_PRE}module` VALUES (11, 'special', '专题', 'special', '', '$url', '', 16, 0, 1, 0, 0, 0, $DT_TIME)");
		
		$db->query("DELETE FROM {$DT_PRE}module WHERE moduleid=12");
		$url = DT_URL.'photo/';
		$db->query("INSERT INTO `{$DT_PRE}module` VALUES (12, 'photo', '图库', 'photo', '', '$url', '', 17, 0, 1, 0, 0, 0, $DT_TIME)");
		
		$db->query("DELETE FROM {$DT_PRE}module WHERE moduleid=13");
		$url = DT_URL.'brand/';
		$db->query("INSERT INTO `{$DT_PRE}module` VALUES (13, 'brand', '品牌', 'brand', '', '$url', '', 13, 0, 1, 0, 0, 0, $DT_TIME)");
		
		$db->query("DELETE FROM {$DT_PRE}module WHERE moduleid=14");
		$url = DT_URL.'video/';
		$db->query("INSERT INTO `{$DT_PRE}module` VALUES (14, 'video', '视频', 'video', '', '$url', '', 18, 0, 1, 0, 0, 0, $DT_TIME)");
		
		$db->query("DELETE FROM {$DT_PRE}module WHERE moduleid=15");
		$url = DT_URL.'down/';
		$db->query("INSERT INTO `{$DT_PRE}module` VALUES (15, 'down', '下载', 'down', '', '$url', '', 19, 0, 1, 0, 0, 0, $DT_TIME)");

		$percent = '50%';
		include 'step_'.$step.'.tpl.php';
	break;
	case '4'://更新系统设置
		//更新会员组	
		$result = $db->query("SELECT * FROM {$DT_PRE}group ORDER BY groupid DESC");
		while($r = $db->fetch_array($result)) {			
			$n = 'group-'.$r['groupid'];
			if(is_file(DT_ROOT.'/file/setting/'.$n.'.php')) {
				$N = include DT_ROOT.'/file/setting/'.$n.'.php';
			} else {
				if($r['vip']) {
					$N = include DT_ROOT.'/file/setting/group-7.php';
				} else {
					$N = include DT_ROOT.'/file/setting/group-6.php';
				}
			}			
			$O = get_setting($n);
			foreach($N as $k=>$v) {
				if(isset($O[$k])) $N[$k] = $O[$k];
			}
			$N = daddslashes($N);
			update_setting($n, $N);
		}
		//更新系统设置
		$dirs = array();
		$result = $db->query("SELECT * FROM {$DT_PRE}module");
		while($r = $db->fetch_array($result)) {
			$i = $r['moduleid'];
			$dirs[] = $r['moduledir'];
			if($r['module'] == 'article') {
				$N = include DT_ROOT.'/file/setting/module-21.php';
			} else if($r['module'] == 'info') {
				$N = include DT_ROOT.'/file/setting/module-22.php';
			} else {
				$N = include DT_ROOT.'/file/setting/module-'.$i.'.php';
			}
			$O = get_setting($i);
			foreach($N as $k=>$v) {
				if(isset($O[$k])) $N[$k] = $O[$k];
			}
			$N = daddslashes($N);
			update_setting($i, $N);
		}		
		$db->query("INSERT INTO {$DT_PRE}setting (item,item_key,item_value) VALUES('destoon','backtime','$DT_TIME')");
		$dirs[] = 'ad';
		$dirs[] = 'announce';
		$dirs[] = 'comment';
		$dirs[] = 'feed';
		$dirs[] = 'link';
		$dirs[] = 'spread';
		$dirs[] = 'vote';
		foreach($dirs as $dir) {
			$files = glob(DT_ROOT.'/'.$dir.'/*.php');
			foreach($files as $file) {
				$content = file_get($file);
				if(strpos($content, 'PARSE_STR') !== false) {
					$content = str_replace('PARSE_STR', 'DT_REWRITE', $content);
					file_put($file, $content);
				}
			}
		}
		cache_clear('php', 'dir', 'tpl');//更新模板缓存
		cache_group();//更新会员组
		cache_module();//更新模块
		$percent = '75%';
		include 'step_'.$step.'.tpl.php';
	break;
	case '5'://更新数据库索引 升级成功
		sql_execute(file_get_contents('index.sql'));
		file_put(DT_ROOT.'/upgrade/index.php', '<meta http-equiv="Cache-Control" content="no-cache"/><meta http-equiv="refresh" content="0;url=../">');
		file_del(DT_CACHE.'/config.inc.php');
		tohtml('index');
		$percent = '100%';
		include 'step_'.$step.'.tpl.php';
	break;
}
?>